home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / jsutil.py < prev    next >
Encoding:
Python Source  |  2007-08-17  |  4.0 KB  |  122 lines

  1. # -*- test-case-name: nevow.test.test_consolejstest -*-
  2. # Copyright (c) 2004-2007 Divmod.
  3. # See LICENSE for details.
  4.  
  5. from twisted.python.procutils import which
  6. from twisted.python.util import sibpath
  7.  
  8. import nevow
  9. from nevow.athena import LivePage, allJavascriptPackages, JSModule
  10.  
  11. _TEST_BOOTSTRAP = LivePage.BOOTSTRAP_MODULES[:]
  12. _TEST_BOOTSTRAP.insert(_TEST_BOOTSTRAP.index('Divmod.Runtime'),
  13.                        'Divmod.MockBrowser')
  14.  
  15. _DUMMY_MODULE_NAME = 'ConsoleJSTest'
  16.  
  17. def getDependencies(fname, ignore=[],
  18.                     bootstrap=_TEST_BOOTSTRAP,
  19.                     packages=None):
  20.     """
  21.     Get the javascript modules that the code in the file with name C{fname}
  22.     depends on, recursively
  23.  
  24.     @param fname: javascript source file name
  25.     @type fname: C{str}
  26.  
  27.     @param ignore: names of javascript modules to exclude from dependency list
  28.     @type ignore: sequence
  29.  
  30.     @param bootstrap: names of javascript modules to always include, regardless
  31.     of explicit dependencies (defaults to L{LivePage}'s list of bootstrap
  32.     modules, plus the mock browser implementation.)
  33.     @type boostrap: sequence
  34.  
  35.     @param packages: all javascript packages we know about.  defaults to the
  36.     result of L{allJavascriptPackages}
  37.     @type packages: C{dict}
  38.  
  39.     @return: modules included by javascript in file named C{fname}
  40.     @rtype: dependency-ordered list of L{JSModule} instances
  41.     """
  42.     if packages is None:
  43.         packages = allJavascriptPackages()
  44.  
  45.     # TODO if a module is ignored, we should ignore its dependencies
  46.     bootstrapModules = [JSModule.getOrCreate(m, packages)
  47.                         for m in bootstrap if m not in ignore]
  48.  
  49.     packages[_DUMMY_MODULE_NAME] = fname
  50.     module = JSModule(_DUMMY_MODULE_NAME, packages)
  51.  
  52.     return (bootstrapModules +
  53.             [dep for dep in module.allDependencies()
  54.              if (dep.name not in bootstrap
  55.                  and dep.name != _DUMMY_MODULE_NAME
  56.                  and dep.name not in ignore)])
  57.  
  58.  
  59.  
  60. def findJavascriptInterpreter():
  61.     """
  62.     Return a string path to a JavaScript interpreter if one can be found in
  63.     the executable path. If not, return None.
  64.     """
  65.     for script in ['smjs', 'js']:
  66.         _jsInterps = which(script)
  67.         if _jsInterps:
  68.             return _jsInterps[0]
  69.     return None
  70.  
  71.  
  72.  
  73. def generateTestScript(fname, after={'Divmod.Base': ('Divmod.Base.addLoadEvent = function() {};',)},
  74.                               dependencies=None):
  75.     """
  76.     Turn the contents of the Athena-style javascript test module in the file
  77.     named C{fname} into a plain javascript script.  Recursively includes any
  78.     modules that are depended on, as well as the utility module
  79.     nevow/test/testsupport.js.
  80.  
  81.     @param fname: javascript source file name
  82.     @type fname: C{str}
  83.  
  84.     @param after: mapping of javascript module names to sequences of lines of
  85.     javascript source that should be injected into the output immediately
  86.     after the source of the named module is included
  87.     @type after: C{dict}
  88.  
  89.     @param dependencies: the modules the script depends on.  Defaults to the
  90.     result of L{getDependencies}
  91.     @type dependencies: dependency-ordered list of L{JSModule}
  92.     instances
  93.  
  94.     @return: converted javascript source text
  95.     @rtype: C{str}
  96.     """
  97.     if dependencies is None:
  98.         dependencies = getDependencies(fname)
  99.  
  100.     load = lambda fname: 'load(%r);' % (fname,)
  101.     initialized = {}
  102.     js = [load(sibpath(nevow.__file__, 'test/testsupport.js'))]
  103.     for m in dependencies:
  104.         segments = m.name.split('.')
  105.         if segments[-1] == '__init__':
  106.             segments = segments[:-1]
  107.         initname = '.'.join(segments)
  108.         if initname not in initialized:
  109.             initialized[initname] = 1
  110.             if '.' in initname:
  111.                 prefix = ''
  112.             else:
  113.                 prefix = 'var '
  114.             js.append('%s%s = {};' % (prefix, initname))
  115.         js.append(load(m.mapping[m.name]))
  116.         if m.name in after:
  117.             js.extend(after[m.name])
  118.  
  119.     js.append(file(fname).read())
  120.  
  121.     return '\n'.join(js)
  122.